<!--  -->
<template>
  <div class="bottom-bar">
    <div class="check-content">
      <CheckButton
        :is-checked="isSelectorAll"
        class="check-button"
        @click.native="checkClick"
      ></CheckButton>
      <span>全选</span>
    </div>
    <div class="price">合计：{{ totalPrice }}</div>
    <div class="calculate">去计算：{{ checkLength }}</div>
  </div>
</template>

<script>
import CheckButton from "components/content/checkButton/CheckButton";

export default {
  name: "",
  components: {
    CheckButton,
  },
  computed: {
    totalPrice() {
      return (
        "¥" +
        this.$store.state.cartList
          .filter((item) => {
            return item.checked;
          })
          .reduce((preValue, item) => {
            return preValue + item.price * item.count;
          }, 0)
          .toFixed(2)
      );
    },
    checkLength() {
      return this.$store.state.cartList.filter((item) => item.checked).length;
    },
    isSelectorAll() {
      // return !(this.$store.state.cartList.filter(item=> !item.checked).length)
      if (this.$store.state.cartList.length === 0) return false;
      return !this.$store.state.cartList.find((item) => !item.checked);
    },
  },
  methods: {
    checkClick() {
      if (this.isSelectorAll) {
        this.$store.state.cartList.forEach(
          (item) => (item.checked = false)
        );
      } else {
        this.$store.state.cartList.forEach(
          (item) => (item.checked = true)
        );
      }
    },
  },
};
</script>

<style scoped>
.bottom-bar {
  display: flex;
  height: 40px;
  background: #eee;
  position: relative;
  line-height: 40px;
  font-size: 14px;
}
.check-content {
  display: flex;
  align-items: center;
  margin-left: 10px;
  width: 60px;
}
.check-button {
  width: 20px;
  height: 20px;
  line-height: 20px;
}
.price {
  font-size: 16px;
  color: red;
  margin-left: 20px;
  flex: 1;
}
.calculate {
  width: 100px;
  background: red;
  color: #fff;
  text-align: center;
}

</style>